﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

using _XNA__RunningDeath.GLO;
using _XNA__RunningDeath.DAO;

namespace _XNA__RunningDeath.GUI
{
    class CGUI_LoadGame:CDiaLogBox
    {
        Texture2D _texBackground;
        Texture2D _texBtnStart;
        Texture2D _texBtnBack;
        Texture2D _bgGameSave;
        Rectangle _recGameDataStand;
        SpriteFont fontFileName;
        int indexSelected = -1;
        //SaveGameData data;

        public CGUI_LoadGame(ContentManager content,GameTime gameTime,SpriteBatch spriteBatch):base(content,gameTime,spriteBatch )
        {
            this._texBackground = this._content.Load<Texture2D>(@"GUI/bgLoadGame");
            this._texBtnStart = this._content.Load<Texture2D>(@"GUI/btnStart");
            this._texBtnBack = this._content.Load<Texture2D>(@"GUI/btnBack");
            this._texBG = this._content.Load<Texture2D>(@"GUI/guiBG");
            this._bgGameSave = this._content.Load<Texture2D>(@"GUI/gb-gamesave");
            this._dstoado = this._content.Load<Dictionary<string, Rectangle>>(@"GUI/ztoadoXMLgui");//
            _recGameDataStand = CThamSo.recDataStand;
            fontFileName = content.Load<SpriteFont>("FontLoadGame");

            createRecGame();

        }

        private void createRecGame()
        {
            SaveGame.loadArrData();
            CGLOBAL.recGameData.Clear();
            for (int i = 0; i < CGLOBAL.arrDataGame.Count; i++)
            {
                if (i == 0)
                {
                    Rectangle recdata = new Rectangle(_recGameDataStand.X, _recGameDataStand.Y, _recGameDataStand.Width, _recGameDataStand.Height);
                    CGLOBAL.recGameData.Add(recdata);
                }
                else
                {
                    _recGameDataStand.Y = _recGameDataStand.Y + 33;
                    Rectangle recdata = new Rectangle(_recGameDataStand.X, _recGameDataStand.Y, _recGameDataStand.Width, _recGameDataStand.Height);
                    CGLOBAL.recGameData.Add(recdata);
                }
            }
        }
        public override void Update(GameTime gameTime)
        {
            CGLOBAL.MousePrev = CGLOBAL.MouseCurrent;
            CGLOBAL.MouseCurrent = Mouse.GetState();

            if(CGLOBAL.MouseCurrent.LeftButton==ButtonState.Pressed && CGLOBAL.MousePrev.LeftButton== ButtonState.Released)
            {
                if(this._dstoado["btnStartLoad"].Contains(new Point(CGLOBAL.MouseCurrent.X,CGLOBAL.MouseCurrent.Y)))
                {
                    if (indexSelected >= 0)
                    {
                        CGLOBAL.Life = CGLOBAL.arrDataGame[indexSelected].Life;
                        CGLOBAL.pointBeMat01 = CGLOBAL.arrDataGame[indexSelected].pointBeMat01;
                        CGLOBAL.pointBeMat02 = CGLOBAL.arrDataGame[indexSelected].pointBeMat02;
                        CGLOBAL.pointBeMat03 = CGLOBAL.arrDataGame[indexSelected].pointBeMat03;
                        CGLOBAL.Score = CGLOBAL.arrDataGame[indexSelected].Score;
                        CGLOBAL.pointDa = CGLOBAL.arrDataGame[indexSelected].pointDa;
                        CGLOBAL._recHo = CGLOBAL.arrDataGame[indexSelected].recHo;
                        CGLOBAL.pointActor = CGLOBAL.arrDataGame[indexSelected].PointActor;
                        CGLOBAL.speedAt = CGLOBAL.arrDataGame[indexSelected].speedAt;
                        

                        CGLOBAL.NewGame = enumGame.mLoad;
                        CGLOBAL.soundFXbtn.Play(CThamSo.volumSound, CThamSo.pitchSound, CThamSo.panSound);
                        CGLOBAL.NextGUI = true;
                        CGLOBAL.GUISelected = GUIenum.guiRunningDeath;
                        
                    }
                }
                else if(this._dstoado["btnBackLoad"].Contains(new Point(CGLOBAL.MouseCurrent.X,CGLOBAL.MouseCurrent.Y)))
                {
                    CGLOBAL.soundFXbtn.Play(CThamSo.volumSound, CThamSo.pitchSound, CThamSo.panSound);
                    CGLOBAL.NextGUI = true;
                    CGLOBAL.GUISelected = GUIenum.guiMainMenu;
                }

                for (int i = 0; i < CGLOBAL.recGameData.Count; i++)
                {
                    if (CGLOBAL.recGameData[i].Contains(new Point(CGLOBAL.MouseCurrent.X, CGLOBAL.MouseCurrent.Y)))
                    {
                        CGLOBAL.soundFXbtn.Play(CThamSo.volumSound, CThamSo.pitchSound, CThamSo.panSound);
                        indexSelected = i;
                    }
                }
            }
        }

        public override void Draw(GameTime gameTime)
        {
            this._spriteBatch.Begin();
            this._spriteBatch.Draw(this._texBG,Vector2.Zero,Color.White);
            this._spriteBatch.Draw(this._texBackground,new Vector2(this._dstoado["bgLoadGame"].X,this._dstoado["bgLoadGame"].Y),Color.White);
            if (indexSelected < 0)
            {
                this._spriteBatch.Draw(this._texBtnStart, new Vector2(this._dstoado["btnStartLoad"].X, this._dstoado["btnStartLoad"].Y), Color.DarkGray);
            }
            else
            {
                this._spriteBatch.Draw(this._texBtnStart, new Vector2(this._dstoado["btnStartLoad"].X, this._dstoado["btnStartLoad"].Y), Color.White);
            }
            this._spriteBatch.Draw(this._texBtnBack, new Vector2(this._dstoado["btnBackLoad"].X, this._dstoado["btnBackLoad"].Y), Color.White);

            for (int i = 0; i < CGLOBAL.recGameData.Count; i++)
            {
                if(i == indexSelected)
                    this._spriteBatch.Draw(this._bgGameSave, CGLOBAL.recGameData[i], Color.YellowGreen);
                else
                    this._spriteBatch.Draw(this._bgGameSave, CGLOBAL.recGameData[i], Color.White);

                string name = addSpace( CGLOBAL.arrDataGame[i].PlayerName,13);
                DateTime date = CGLOBAL.arrDataGame[i].Date;
                string mdate = addSpacePrev( date.Day.ToString(),2) + "/" + addSpacePrev( date.Month.ToString(),2) + "/" + addSpacePrev( date.Year.ToString(),4);
                string mOclo = addSpacePrev(date.Hour.ToString(),2) + ":" + addSpacePrev( date.Minute.ToString(),2) + ":" + addSpacePrev( date.Second.ToString(),2);
                //mdate = addSpacePrev(mdate, 10);
               // mOclo = addSpacePrev(mOclo, 8);

                mdate = mOclo + " " + mdate;
                _spriteBatch.DrawString(fontFileName, "->" + name + " :" + mdate, new Vector2(CGLOBAL.recGameData[i].X, CGLOBAL.recGameData[i].Y), Color.Black);
            }
            
            this._spriteBatch.End();
        }

        /// <summary>
        /// Hàm này có chức năng là sẽ add khoảng trắng vào một chuỗi đã truyền vào sao cho chuổi trả về là chuỗi có sl ký tự 
        /// bằng với số lượng ta yêu cầu
        /// </summary>
        /// <param name="s">Chuổi S cần xử lý</param>
        /// <param name="sl">Số lượng ký tự trong chuỗi s cần đạt được</param>
        /// <returns>Chuỗi s sau khi đã xử lý</returns>
        private string addSpace(string s, int sl)
        {
            int count = sl - s.Length;

            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    s += " ";
                }
            }

            return s;
        }

        private string addSpacePrev(string s, int sl)
        {
            int count = sl - s.Length;

            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    s = "0"+s;
                }
            }

            return s;
        } 

        /// <summary>
        /// hàm này sẽ canh chữ theo số lượng ký tự truyền vào 
        /// ví dụ ta cần số có 3 chữ số mà truyền vào 30 - > _30
        /// </summary>
        /// <param name="s">chuỗi s cần xử lý</param>
        /// <param name="sl">độ rộng của chuỗi cần đạt được nên truyền vào số lẽ</param>
        /// <returns></returns>
        private string centerText(string s, int sl)
        {
            int count = sl - s.Length;
            if(count > 0)
            {
                //int div = count / 2;
                for(int i = 0; i<count; i++)
                {
                    s = "0" + s;
                }
            }
            return s;
        }
    }
}
